package model

import "gorm.io/gorm"

type Good struct {
	gorm.Model
	UserId int64   `gorm:"type:int;not null;commit:用户Id"`
	Title  string  `gorm:"type:varchar(150);not null;commit:标题"`
	Image  string  `gorm:"type:varchar(150); null;commit:图片"`
	Price  float64 `gorm:"type:decimal(10,2);not null;commit:价格"`
	Sales  int     `gorm:"type:int;not null;commit:销量"`
	Stock  int     `gorm:"type:int;not null;commit:库存"`
	Status int     `gorm:"type:int;not null;commit:商品状态 0-未上架,1-已上架"`
	TypeId int     `gorm:"type:int;not null;commit:分类商品Id"`
}
type Shopwindow struct {
	gorm.Model
	UserId int64   `gorm:"type:int;not null;commit:用户Id"`
	GoodId int64   `gorm:"type:int;not null;commit:商品Id"`
	Title  string  `gorm:"type:varchar(150);not null;commit:标题"`
	Image  string  `gorm:"type:varchar(150); null;commit:图片"`
	Price  float64 `gorm:"type:decimal(10,2);not null;commit:价格"`
	Sales  int     `gorm:"type:int;not null;commit:销量"`
	Stock  int     `gorm:"type:int;not null;commit:库存"`
	Status int     `gorm:"type:int;not null;commit:商品状态 0-未上架,1-已上架"`
	TypeId int     `gorm:"type:int;not null;commit:分类商品Id"`
}
type GoodShopwindow struct {
	gorm.Model
	UserId           int64   `gorm:"type:int;not null;commit:用户Id"`
	GoodId           int64   `gorm:"type:int;not null;commit:商品Id"`
	GoodShopwindowId int     `gorm:"type:int;not null;commit:商品橱窗Id"`
	Title            string  `gorm:"type:varchar(150);not null;commit:标题"`
	Image            string  `gorm:"type:varchar(150); null;commit:图片"`
	Price            float64 `gorm:"type:decimal(10,2);not null;commit:价格"`
	Sales            int     `gorm:"type:int;not null;commit:销量"`
	Stock            int     `gorm:"type:int;not null;commit:库存"`
	Status           int     `gorm:"type:int;not null;commit:商品状态 0-未上架,1-已上架"`
	TypeId           int     `gorm:"type:int;not null;commit:分类商品Id"`
}
type ShopwindowType struct {
	gorm.Model
	Name string `gorm:"type:varchar(150);not null;commit:分类名称"`
}

// //分类创建 + 关联商品
type GoodTypeList struct {
	UserId int64
	Title  string
	Image  string
	Price  float64
	Sales  int
	Stock  int
	Name   string
}
type Order struct {
	gorm.Model
	Title  string `gorm:"type:varchar(150);not null;commit:标题"`
	UserId int64  `gorm:"type:int;not null;commit:用户Id"`
	GoodId int64  `gorm:"type:int;not null;commit:商品Id"`
	Status int64  `gorm:"type:int;not null;commit:商品状态 0-未下单,1-已下单"`
}

func (u *GoodShopwindow) CreateGoodShopwindow(db *gorm.DB) error {
	err := db.Create(&u).Error
	return err
}
func (u *ShopwindowType) CreateShopwindowType(db *gorm.DB) error {
	err := db.Create(&u).Error
	return err
}
func (u *GoodTypeList) ShopwindowTypeList(db *gorm.DB) (goodTypeList []*GoodTypeList, err error) {
	db.Model(&Good{}).
		Select("goods.user_id,goods.title,goods.image,goods.price,goods.sales,goods.stock,shopwindow_types.name").
		Joins("left join shopwindow_types on goods.type_id = shopwindow_types.id").
		Find(&goodTypeList)
	return
}

// 商品状态
func (u *Good) CheckGoodStaus(db *gorm.DB, id, status int) error {
	err := db.Model(&Good{}).Where("id=?", id).Update("status", status).Error
	return err
}

// 商品橱窗状态
func (u *GoodShopwindow) CheckGoodShopwindowStatus(db *gorm.DB, id, status int) error {
	err := db.Model(&GoodShopwindow{}).Where("id=?", id).Update("status", status).Error
	return err
}
func (u *Order) CreateOrder(db *gorm.DB) error {
	err := db.Create(&u).Error
	return err
}
